/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.aice.chinamapview.component;

import com.aice.chinamapview.ResourceTable;
import com.noteam.chinamapview.util.ColorChangeUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * CoordinatorBottomLayout
 *
 * @author jjy
 * @since 2021-05-22
 */
public class CoordinatorBottomLayout extends DirectionalLayout {
    private static final int INDEXTWO = 2;
    private static final int INDEXTHREE = 3;
    private static final int INDEXFOUR = 4;
    private ListContainer listContainer;
    private List<String> list;
    private MyLiStItemProVider myLiStItemProVider;
    private String spliteString = "_";

    /**
     *
     * 构造函数
     *
     * @param context context对象
     */
    public CoordinatorBottomLayout(Context context) {
        super(context);
        initView();
    }

    /**
     *
     * 构造函数
     *
     * @param context context对象
     * @param attrSet 属性参数
     */
    public CoordinatorBottomLayout(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    /**
     *
     * 构造函数
     *
     * @param context context对象
     * @param attrSet 属性参数
     * @param styleName styleName名称
     */
    public CoordinatorBottomLayout(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    private List<String> initData() {
        list = new ArrayList<>();
        for (int index = 0; index < ColorChangeUtil.getProvinceDatas().length; index++) {
            list.add(ColorChangeUtil.getProvinceDatas()[index]);
        }

        return list;
    }

    private void initView() {
        Component layoutScatter = LayoutScatter.getInstance(mContext).
                parse(ResourceTable.Layout_coordinator_bottom_layout, this, true);
        listContainer = (ListContainer) layoutScatter.findComponentById(ResourceTable.Id_listcontainer);
        myLiStItemProVider = new MyLiStItemProVider(mContext, initData());
        listContainer.setItemProvider(myLiStItemProVider);
        listContainer.setLongClickable(false);
        listContainer.setReboundEffect(false);
        invalidate();
    }

    /**
     *
     * 根据省份名称刷新数据
     *
     * @param provinceName 省份名称
     */
    public void notifyData(String provinceName) {
        for (int index = 0; index < list.size(); index++) {
            if (list.get(index).contains(provinceName)) {
                String string = list.get(index);
                list.remove(index);
                list.add(0, string);
                myLiStItemProVider.notifyDataChanged();
            }
        }
    }

    /**
     * MyLiStItemProVider
     *
     * @author jjy
     * @since 2021-05-12
     */
    private class MyLiStItemProVider extends BaseItemProvider {
        private List<String> datas;

        MyLiStItemProVider(Context context, List<String> datas) {
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int index) {
            return datas.get(index);
        }

        @Override
        public long getItemId(int index) {
            return index;
        }

        @Override
        public Component getComponent(int i, Component convertComponent, ComponentContainer componentContainer) {
            ViewHolder holder = null;
            Component component = null;
            if (holder == null) {
                component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_list_item,
                        null, false);
                holder = new ViewHolder();
                holder.provideName = (Text) component.findComponentById(ResourceTable.Id_provideName);
                holder.powerLength = (Text) component.findComponentById(ResourceTable.Id_powerLength);
                holder.totalLength = (Text) component.findComponentById(ResourceTable.Id_totalLength);
                holder.powerUserTime = (Text) component.findComponentById(ResourceTable.Id_powerUserTime);
                holder.totalUseTime = (Text) component.findComponentById(ResourceTable.Id_totalUseTime);
                component.setTag(holder);
            }
            holder.provideName.setText(datas.get(i).split(spliteString)[0]);
            if (i == 0) {
                holder.provideName.setTextColor(Color.RED);
            }
            holder.powerLength.setText(datas.get(i).split(spliteString)[1] + "%");
            holder.totalLength.setText(datas.get(i).split(spliteString)[INDEXTWO] + "%");
            holder.powerUserTime.setText(String.valueOf(datas.get(i).split(spliteString)[INDEXTHREE]));
            holder.totalUseTime.setText(String.valueOf(datas.get(i).split(spliteString)[INDEXFOUR]));
            return component;
        }
    }

    /**
     * ViewHolder
     *
     * @author jjy
     * @since 2021-05-12
     */
    static class ViewHolder {
        Text provideName;
        Text powerLength;
        Text totalLength;
        Text powerUserTime;
        Text totalUseTime;
    }
}
